package com.polidea.rxohosble2.internal.util;


import bleshadow.javax.inject.Inject;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;
import io.reactivex.schedulers.Schedulers;
import ohos.app.Context;

public class LocationServicesOkObservableApi4Factory {
    final Context context;
    final LocationServicesStatus locationServicesStatus;

    @Inject
    LocationServicesOkObservableApi4Factory(
            final Context context,
            final LocationServicesStatus locationServicesStatus) {
        this.context = context;
        this.locationServicesStatus = locationServicesStatus;
    }

    public Observable<Boolean> get() {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(final ObservableEmitter<Boolean> emitter) {
                final boolean initialValue = locationServicesStatus.isLocationProviderOk();
//                final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
//                    @Override
//                    public void onReceive(Context context, Intent intent) {
//                        final boolean newValue = locationServicesStatus.isLocationProviderOk();
//                        emitter.onNext(newValue);
//                    }
//                };
                emitter.onNext(initialValue);
//                context.registerReceiver(broadcastReceiver, new IntentFilter(LocationManager.MODE_CHANGED_ACTION));
                emitter.setCancellable(new Cancellable() {
                    @Override
                    public void cancel() {
//                        context.unregisterReceiver(broadcastReceiver);
                    }
                });
            }
        })
                .distinctUntilChanged()
                .subscribeOn(Schedulers.trampoline())
                .unsubscribeOn(Schedulers.trampoline());
    }

}
